/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import router from '@system.router'
import usbManager from '@ohos.usbManager';
import CheckEmptyUtils from '../../utils/CheckEmptyUtils.js';

var deviceList;

export default {
    data: {
        result: 'World',
        index: 0,
        device: null,
        deviceInfo: null,
        pip: null
    },
    onInit() {
        this.getDeviceList();
    },
    getDeviceList() {
        console.info("usb getDeviceList...")
        deviceList = usbManager.getDevices();
        if (CheckEmptyUtils.isEmptyArr(deviceList)) {
            console.info("usb getDeviceList is null")
            this.result = this.$t('device list is empty');
            return;
        }
        this.device = deviceList[0];
        this.deviceInfo = "设备名称：" + this.device.name + "\\n 产商信息: " + this.device.manufacturerName;
        var devices = JSON.stringify(deviceList);
        console.info("get devices result : " + devices)
    },
    openDevice() {
        if (CheckEmptyUtils.isEmpty(this.device)) {
            this.result = "请先确保USB设备已插上，设备信息不为空";
            console.info("usb openDevice device is null");
            return;
        }
        if (!CheckEmptyUtils.isEmpty(this.pip)) {
            this.result = "设备已打开，请不要重复打开";
            return;
        }
        try {
            this.pip = usbManager.connectDevice(this.device);
        } catch (err) {
            this.result = "设备打开失败：" + err;
            console.info("usb 设备打开失败：" + err);
            return;
        }

        console.info("usb connect back pip: " + JSON.stringify(this.pip));
        if (!CheckEmptyUtils.isEmpty(this.pip)) {
            this.result = "device打开成功。\npipe信息: " + JSON.stringify(this.pip);
        } else {
            this.result = "device打开失败";
        }
    },
    setConfig() {
        if (CheckEmptyUtils.isEmpty(this.device)) {
            this.result = "请先确保USB设备已插上，设备信息不为空";
            return;
        }
        if (CheckEmptyUtils.isEmpty(this.pip)) {
            this.result = "请先打开设备";
            return;
        }
        var status = usbManager.setConfiguration(this.pip, this.device.configs[0]);
        this.result = "设置设备配置返回码：" + status;
        console.info("usb setConfiguration status : " + status);
    },
    setInterface() {
        if (CheckEmptyUtils.isEmpty(this.device)) {
            this.result = "请先确保USB设备已插上，设备信息不为空";
            return;
        }
        if (CheckEmptyUtils.isEmpty(this.pip)) {
            this.result = "请先打开设备";
            return;
        }
        var claimStatus = usbManager.claimInterface(this.pip, this.device.configs[0].interfaces[0], true);
        if (claimStatus == 0) {
            var status = usbManager.setInterface(this.pip, this.device.configs[0].interfaces[0]);
            this.result = "设置设备接口返回码：" + status;
            console.info("usb setInterface status : " + status);
        } else {
            this.result = "设置设备接口claimInterface失败：" + claimStatus;
            console.info("usb claimInterface status : " + claimStatus);
        }
    },
    closeDevice() {
        if (CheckEmptyUtils.isEmpty(this.pip)) {
            this.result = "请先打开设备";
            console.info("usb closeDevice device not open");
            return;
        }
        var resultCode = usbManager.closePipe(this.pip);
        this.pip = null;
        if (resultCode == 0) {
            this.result = "device关闭成功";
        } else {
            console.info("usb closeDevice device faild resultCode: " + resultCode);
            this.result = "device关闭失败，错误码： " + resultCode;
        }
    },
    onBackPress() {
        if (!CheckEmptyUtils.isEmpty(this.pip)) {
            usbManager.closePipe(this.pip);
            this.pip = null;
        }
        router.back();
    },
}
